package com.lwj.service.impl;
 
import com.lwj.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisConnectionUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
 
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
 
 
@Service("redisService")
public class RedisServiceImpl implements RedisService {
 
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
 
    /**
     * 批量保存list列表 如果存在则先清空，再插入
     *
     * @param key
     * @param value
     */
    public void delAndSaveList(String key, List value) {
        try {
            ListOperations<String, Object> opsForList = redisTemplate.opsForList();
            while (opsForList.size(key) > 0) {
                redisTemplate.opsForList().leftPop(key);
            }
            opsForList.rightPushAll(key, value);
        } finally {
            RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());
        }
    }
 
    /**
     * 插入map对象
     *
     * @param key
     * @param value
     */
    public void saveMap(String key, Map value) {
        try {
            redisTemplate.opsForHash().putAll(key, value);
        } finally {
            RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());
        }
    }
 
    /**
     * 获取map对象
     *
     * @param key
     * @return
     */
    public Map getMap(String key) {
        try {
            return redisTemplate.opsForHash().entries(key);
        } finally {
            RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());
        }
    }
 
    /**
     * 删除map对象
     *
     * @param key
     */
    public void delMap(String key) {
        try {
            redisTemplate.opsForHash().delete(key);
        } finally {
            RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());
        }
    }
 
    /**
     * 删除map中某些值
     *
     * @param key
     * @param hashKeys
     */
    public void delKeyMap(String key, String... hashKeys) {
        try {
            redisTemplate.opsForHash().delete(key, hashKeys);
        } finally {
            RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());
        }
    }
 
    /**
     * @param key
     * @param value
     * @param expireSecond
     */
    public void saveStringValue(String key, String value, Long expireSecond) {
        if (existsKey(key)) {
            delKey(key);
        }
        redisTemplate.opsForValue().set(key, value, expireSecond, TimeUnit.MINUTES);
    }
 
 
    /**
     * @param key
     * @return
     */
    public boolean existsKey(final String key) {
        return (Boolean) redisTemplate.execute(new RedisCallback() {
            public Object doInRedis(RedisConnection redisConnection) throws DataAccessException {
                return redisConnection.exists(key.getBytes());
            }
        });
 
    }
 
    /**
     * @param keys
     */
    public long delKey(final String... keys) {
        return (Long) redisTemplate.execute(new RedisCallback() {
            public Long doInRedis(RedisConnection connection) throws DataAccessException {
                long result = 0;
                for (int i = 0; i < keys.length; i++) {
                    result = connection.del(keys[i].getBytes());
                }
                return result;
            }
        });
    }
 
}